package com.david.system.service.impl;

import com.david.common.constant.AdminConstants;
import com.david.common.exception.RePasswordException;
import com.david.system.pojo.DTO.RePasswordDTO;
import com.david.system.pojo.Entity.MenuEntity;
import com.david.system.pojo.Entity.RoleEntity;
import com.david.system.pojo.Entity.UserEntity;
import com.david.system.pojo.VO.MenuWithRoleVO;
import com.david.system.pojo.Entity.UserInfo;
import com.david.system.mapper.AuthorityMapper;
import com.david.system.mapper.UserMapper;
import com.david.system.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AuthorityMapper authorityMapper;



    /**
    * @Description: SrpingScurity身份认证
    * @Param: [username]
    * @return: com.david.system.Entity.UserEntity
    * @Author: david
    * @Date: 2024/5/27
    */
    @Override
    public UserEntity findByName(String username) {
        UserEntity userEntity = userMapper.selectUserByUsername(username);
        return userEntity;
    }

    /**
    * @Description: 通过角色名称获取路径权限，在动态权限中使用
    * @Param: []
    * @return: java.util.List<com.david.system.Entity.VO.MenuWithRoleVO>
    * @Author: david
    * @Date: 2024/5/27
    */
    @Override
    public List<MenuWithRoleVO> getMenuWithRole() {
        List<MenuEntity> menuEntityList= authorityMapper.selectMenuAll();
        List<MenuWithRoleVO> menuWithRoleVOList = new ArrayList<>();
        for (MenuEntity menu : menuEntityList){
            if (menu.getPath() != null) {
                List<RoleEntity> roleEntities = authorityMapper.selectRoleCodeByMenuId(menu.getId());
                MenuWithRoleVO menuWithRoleVO = new MenuWithRoleVO();
                menuWithRoleVO.setPath(menu.getPath());
                menuWithRoleVO.setRoleEntities(roleEntities);
                menuWithRoleVOList.add(menuWithRoleVO);
            }
        }
        System.out.println(menuWithRoleVOList);
        return menuWithRoleVOList;
    }

    /**
    * @Description: 用户修改密码
    * @Param: [rePasswordDTO]
    * @return: void
    * @Author: david
    * @Date: 2024/5/28
    */
    @Override
    public void changePassword(RePasswordDTO rePasswordDTO) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String username = authentication.getName();
        UserEntity userEntity = authorityMapper.selectUserByUsername(username);
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(rePasswordDTO.getPassword(), userEntity.getPassword());
        String encodePassword = bCryptPasswordEncoder.encode(rePasswordDTO.getNewpassword());
        if (matches){
            authorityMapper.updatePassword(encodePassword, username);
        } else {
            throw new RePasswordException("修改密码失败");
        }
    }

    @Override
    public UserInfo getUserInfo(String username) {
        UserInfo userInfo = userMapper.getUserInfo(username);
        // 通过roleName→roleId→menuId where type in {"3"}
        Integer roleId = userMapper.selectRoleIdByName(userInfo.getRole());
        List<String> permissions = userMapper.selectMenuPermByRoleId(roleId, AdminConstants.btn_Permission);
        List<String> components = userMapper.selectMenuPageByRoleId(roleId, AdminConstants.page_Permission);
//        List<ComponentsVO> components = userMapper.selectMenuPageByRoleId(roleId, AdminConstants.page_Permission);//
//        List<String> componentsToSting = new ArrayList<>();
//        for (ComponentsVO componentsVO : components){
//            componentsToSting.add(componentsVO.toString());
//        }
        System.out.println(components);
        userInfo.setPermissions(permissions);
        userInfo.setComponents(components);

        return userInfo;
    }


}
